<template>
    <div class="indexUser">
        <div class="btn" @click="toLogin" v-if="!getToken()">
            <div class="btnText">
                登录/注册
            </div>
        </div>
        <div class="user" v-if="getToken()">
            <div class="userImage" @mouseenter="flag = true" @mouseleave="flag = false">
                <img src="@/assets/user.png" alt="">
            </div>
        </div>
        <div class="userMenu" @mouseenter="flag = true" @mouseleave="flag = false" v-show="flag">
            <div class="nickname">
                <div class="nameInput">
                    <input type="text" style="height: 24px;width: 42px;border: 0;background: #F6FFFE;"
                        v-model="nickname" ref="nameInput">
                </div>
                <div class="edit" @click="editName">
                    <i class="iconfont icon-bianji"></i>
                </div>
            </div>
            <div class="functionBtn">
                <div class="btnIcon">
                    <i class="iconfont icon-zhishiku"></i>
                </div>
                <div class="btnText">
                    知识库管理
                </div>
            </div>
            <div class="functionBtn">
                <div class="btnIcon">
                    <i class="iconfont icon-shezhi"></i>
                </div>
                <div class="btnText">
                    系统设置
                </div>
            </div>
            <div class="functionBtn">
                <div class="btnIcon">
                    <i class="iconfont icon-lianxiwomen"></i>
                </div>
                <div class="btnText">
                    联系我们
                </div>
            </div>
            <div class="functionBtn" @click="loginout">
                <div class="btnIcon">
                    <i class="iconfont icon-tuichudenglu"></i>
                </div>
                <div class="btnText">
                    退出登录
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { getToken, removeToken } from '@/utils/setToken';
import { ElMessage } from 'element-plus'
import { ref, onBeforeMount } from 'vue';
import { useRouter } from 'vue-router';

//实例化路由
const router = useRouter()

//全局变量
const nickname = ref('Xyz')
const nameInput = ref(null)
const flag = ref(false)

//编辑姓名
const editName = () => {
    nameInput.value?.focus()
}

//登录跳转
const toLogin = () => {
    router.push({ path: '/login' })
}

//退出登录
const loginout = () => {
    removeToken()
    ElMessage({
        type: "success",
        message: "退出登录成功！",
        duration: 1500
    })
    setTimeout(() => {
        router.push({ path: '/begin' })
    }, 1000)
}

onBeforeMount(() => {
    nickname.value = localStorage.getItem('user')
})
</script>

<style lang="scss" scoped>
.indexUser {
    position: relative;

    .btn {
        display: flex;
        justify-content: flex-end;
        cursor: pointer;

        .btnText {
            width: 141px;
            height: 55px;
            line-height: 55px;
            text-align: center;
            color: #fff;
            border-radius: 20px;
            background-color: #000;
        }
    }

    .user {
        display: flex;
        justify-content: flex-end;

        .userImage {
            width: 151px;
            height: 70px;

            img {
                width: 100%;
                height: 100%;
            }
        }
    }

    .userMenu {
        width: 128px;
        height: 188px;
        background: #DEF4F8;
        box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.3);
        border-radius: 5px;
        padding-top: 10px;
        position: absolute;
        right: 0;
        top: 60px;

        .nickname {
            display: flex;
            align-items: center;
            padding-left: 20px;
            cursor: pointer;

            .nameInput {
                width: 42px;
                height: 24px;
                margin-right: 10px;
            }
        }

        .functionBtn {
            display: flex;
            align-items: center;
            font-size: 10px;
            height: 32px;
            width: 119px;
            border-radius: 5px;
            line-height: 32px;
            margin: 5px auto;
            padding-left: 10px;
            cursor: pointer;

            &:hover {
                background: rgb(191, 217, 228);
            }

            .btnIcon {
                margin-right: 10px;
            }
        }
    }
}
</style>